+use std::collections::HashSet;
+
+use core::source::Source;
use ops;
-use util::CargoResult;
+use sources::PathSource;
+use util::{CargoResult, human};
pub struct DocOptions<'a> {
pub all: bool,
pub fn doc(manifest_path: &Path,
options: &mut DocOptions) -> CargoResult<()> {
+ let mut source = PathSource::for_path(&manifest_path.dir_path());
+ try!(source.update());
+ let package = try!(source.get_root_package());
+
+ let mut lib_names = HashSet::new();
+ let mut bin_names = HashSet::new();
+ for target in package.get_targets().iter().filter(|t| t.get_profile().is_doc()) {
+ if target.is_lib() {
+ assert!(lib_names.insert(target.get_name()));
+ } else {
+ assert!(bin_names.insert(target.get_name()));
+ }
+ }
+ for bin in bin_names.iter() {
+ if lib_names.contains(bin) {
+ return Err(human("Cannot document a package where a library and a \
+ binary have the same name. Consider renaming one \
+ or marking the target as `doc = false`"))
+ }
+ }
+
try!(ops::compile(manifest_path, &mut options.compile_opts));
Ok(())
}
fn rustdoc(package: &Package, target: &Target, cx: &mut Context) -> Work {
- // Can't document binaries, but they have a doc target listed so we can
- // build documentation of dependencies even when `cargo doc` is run.
- if target.is_bin() {
- return proc() Ok(())
- }
-
let kind = KindTarget;
let pkg_root = package.get_root();
let cx_root = cx.layout(kind).proxy().dest().dir_path().join("doc");
assert_that(&p.root().join("target/doc/foo/index.html"), existing_file());
})
-test!(no_build_main {
+test!(doc_no_libs {
let p = project("foo")
.file("Cargo.toml", r#"
[package]
name = "foo"
version = "0.0.1"
authors = []
- "#)
- .file("src/lib.rs", r#"
- pub fn foo() {}
- "#)
- .file("src/main.rs", r#"
- bad code
- "#);
- assert_that(p.cargo_process("cargo-doc"),
- execs().with_status(0).with_stdout(format!("\
-{compiling} foo v0.0.1 (file:{dir})
-",
- compiling = COMPILING,
- dir = p.root().display()).as_slice()));
-})
-
-test!(doc_no_libs {
- let p = project("foo")
- .file("Cargo.toml", r#"
- [package]
+ [[bin]]
name = "foo"
- version = "0.0.1"
- authors = []
+ doc = false
"#)
.file("src/main.rs", r#"
bad code
assert_that(&p.root().join("target/doc"), existing_dir());
assert_that(&p.root().join("target/doc/bar/index.html"), existing_file());
+ assert_that(&p.root().join("target/doc/foo/index.html"), existing_file());
+})
+
+test!(doc_lib_bin_same_name {
+ let p = project("foo")
+ .file("Cargo.toml", r#"
+ [package]
+ name = "foo"
+ version = "0.0.1"
+ authors = []
+ "#)
+ .file("src/main.rs", "fn main() {}")
+ .file("src/lib.rs", "fn foo() {}");
+
+ assert_that(p.cargo_process("cargo-doc"),
+ execs().with_status(101)
+ .with_stderr("\
+Cannot document a package where a library and a binary have the same name. \
+Consider renaming one or marking the target as `doc = false`
+"));
})